package com.ua.open.common.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.stereotype.Service;

import com.ua.common.util.StringHelper;
import com.ua.open.common.persistence.CommonMapper;


@Service
public class CommonService {

	@Autowired
	private CommonMapper commonMapper;
	
	@Autowired
	private DataFieldMaxValueIncrementer sequence;

	/**
	 * 获取sequence序列号
	 * @return
	 */
	public String getCode(){
		String nextSeq = sequence.nextStringValue();
		String SEQ_ZERO = "0000000";
		String tempSeq = (String)nextSeq;
		if(null != tempSeq && tempSeq.length() > 0) {
			if(tempSeq.length() < "0000000".length()) {
				//序列号不足7位，在前面补0
				tempSeq = SEQ_ZERO.substring(0, SEQ_ZERO.length()-tempSeq.length()) + tempSeq;
			}				
		}		
		nextSeq = tempSeq;
		return nextSeq;
	}
	
	/**
	 * 获取数据库当前时间
	 * @return
	 */
	public Timestamp getDatabaseCurrentTime(){
		return commonMapper.getDatabaseCurrentTime();
	}
	
	/**
	 * 获取某个表当前可用的树型编码
	 * @param table 表名(不能为空)
	 * @param field treecode字段名(不能为空)
	 * @param treeCodeLength  treecode编码长度(可以为空 默认值为4)
	 * @param preTreeCode 上级节点TreeCode(可以为空)
	 * @return  最大treecode值
	 */
	public String getMaxTreeCode(String table, String field, String treeCodeLength, String preTreeCode) {
		String maxTreeCode = null;
		String treeCode = "";
		if (table != null && !table.trim().equals("") && 
			field != null && !field.trim().equals("")) {
			StringBuilder sql = new StringBuilder();
			String tableName = table.trim();
			String fieldName = field.trim();
        	if (treeCodeLength == null) {
        		//默认treecode编码长度为4
        		treeCodeLength = "4";
        	}  
        	
        	preTreeCode = StringHelper.convertStringNull(preTreeCode);	 
        	
			sql.append("select max(").append(fieldName).append(") from ");
			sql.append(tableName);
			
			if (preTreeCode != null && !preTreeCode.trim().equals("")) {
				//获取该级节点的下级最大TreeCode
				sql.append(" where length(").append(fieldName).append(") = ").append(Integer.toString(preTreeCode.length()))
				.append("+").append(treeCodeLength).append(" and ").append(fieldName).append(" like '").append(preTreeCode).append("%'");
			} else {
				//获取顶级最大TreeCode
				sql.append(" where length(").append(fieldName).append(") = ").append(treeCodeLength);
			}							
			
			Map<String, String> param = new HashMap<String, String>();
			param.put("sql", sql.toString());
			
			maxTreeCode = commonMapper.getMaxTreeCode(param);
			if (maxTreeCode != null && !maxTreeCode.trim().equals("")) {
			   maxTreeCode = maxTreeCode.trim();
			   String left = maxTreeCode.substring(0, preTreeCode.length());
			   String right = maxTreeCode.substring(maxTreeCode.length() - Integer.parseInt(treeCodeLength), maxTreeCode.length());
			   int ri = Integer.parseInt(right) + 1;
			   right = String.format("%0" + treeCodeLength + "d", ri);
			   treeCode = left + right;
			} else {
				String right = String.format("%0" + treeCodeLength + "d", 1);
				treeCode = preTreeCode + right;
			}
		}
		return treeCode;
	}
}
